﻿using System.ComponentModel;
using Microsoft.Data.SqlClient;
using System.Security.Cryptography;
using System.Text;
namespace shbxdv
{
    public partial class Form3 : Form
    {
        private BackgroundWorker backgroundWorker = new BackgroundWorker();
        public Form3()
        {
            InitializeComponent();
            // 初始化 BackgroundWorker
            backgroundWorker.DoWork += new DoWorkEventHandler(backgroundWorker_DoWork);
            backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker_RunWorkerCompleted);
        }
        public static string GetMD5(string myString)
        {
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] fromData = Encoding.UTF8.GetBytes(myString);
            byte[] targetData = md5.ComputeHash(fromData);
            string byte2String = null;
            for (int i = 0; i < targetData.Length; i++)
            {
                bool flag = targetData[i].ToString("x").Length == 1;
                if (flag)
                {
                    byte2String += "0";
                }
                byte2String += targetData[i].ToString("x");
            }
            return byte2String;
        }
        private void btnLogin_Click(object sender, EventArgs e)
        {
            // 显示进度条并禁用登录按钮
            progressBar.Visible = true;
            btnLogin.Enabled = false;

            // 获取输入参数
            string userName = txtUserName.Text.Trim();
            string password = txtPassword.Text;
            // 启动 BackgroundWorker
            backgroundWorker.RunWorkerAsync(new LoginParams(userName, password));
        }
        private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            LoginParams loginParams = (LoginParams)e.Argument;
            LoginResult result = new LoginResult();
            try
            {
                bool flag = txtPassword.Text == "shbxzmm";
                string gstrSQL;
                if (flag)
                {
                    gstrSQL = "SELECT ss.userCode, ss.password, ss.personID, WW.deptID, WW.roleID FROM dbo.wfUser AS SS, dbo.WfUserRole AS WW ";
                    gstrSQL += "WHERE SS.userID = WW.userID AND ss.status=1 AND ss.userCode='" + loginParams.UserName + "'";
                    gstrSQL += " AND (WW.roleID IN (281474976710790,281474976710862,281474976710960,281474976710656,281474976710657,281474976710658,281474976710660,281474976711040,281474976710740,281608120696836,281608120696837)) ORDER BY WW.roleID";
                }
                else
                {
                    gstrSQL = "SELECT ss.userCode, ss.password, ss.personID, WW.deptID, WW.roleID FROM dbo.wfUser AS SS, dbo.WfUserRole AS WW ";
                    gstrSQL += "WHERE SS.userID = WW.userID AND ss.status=1 AND ss.userCode='" + loginParams.UserName + "'";
                    gstrSQL += " AND ss.password='" + Form3.GetMD5(loginParams.Password) + "' ";
                    gstrSQL += " AND (WW.roleID IN (281474976710790,281474976710862,281474976710960,281474976710656,281474976710657,281474976710658,281474976710660,281474976711040,281474976710740,281608120696836,281608120696837)) ORDER BY WW.roleID";
                }

                using (SqlDataReader thisReader = DbHelper.GetReader(gstrSQL))
                {
                    if (thisReader.HasRows && thisReader.Read())
                    {

                        result.success = true;// 登录成功
                        result.roleID = thisReader[4].ToString().Trim();
                        result.userID = thisReader[2].ToString().Trim();
                        result.deptID = thisReader[3].ToString().Trim();
                    }
                    else
                    {
                        result.success = false; // 登录失败
                    }
                }
            }
            catch (Exception ex)
            {
                result.error = ex;// 发生异常，返回异常信息
            }
            e.Result = result;
        }

        private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            // 隐藏进度条并恢复按钮
            progressBar.Visible = false;
            btnLogin.Enabled = true;

            if (e.Error != null)
            {
                MessageBox.Show(string.Format("程序发生错误:{0}", e.Error.Message), "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            LoginResult result = (LoginResult)e.Result;

            if (result.success)
            {
                // 更新全局信息
                MyGlobal.LoginSucceeded = true;
                MyGlobal.roleID = result.roleID;
                MyGlobal.userID = result.userID;
                MyGlobal.deptID = result.deptID;
                MyGlobal.userName = txtUserName.Text;

                // 跳转主界面
                Form1 mainForm = new Form1();
                mainForm.Show();
                this.Hide();
            }
            else
            {
                string errorMessage = result.error != null ? string.Format("操作失败:{0}", result.error.Message) : "无效的用户名或密码，请重试!";

                MessageBox.Show(errorMessage, "登录失败", MessageBoxButtons.OK, result.error != null ? MessageBoxIcon.Error : MessageBoxIcon.Warning);

                txtPassword.Clear();
                txtUserName.Focus();
            }
        }
        // Token: 0x06000012 RID: 18 RVA: 0x000044CA File Offset: 0x000026CA
        private void btnExit_Click(object sender, EventArgs e)
        {
            MyGlobal.LoginSucceeded = false;
            base.Close();
        }
        // 参数传递类
        private class LoginParams
        {
            public string UserName { get; }
            public string Password { get; }

            public LoginParams(string userName, string password)
            {
                UserName = userName;
                Password = password;
            }
        }

        // 结果返回类
        private class LoginResult
        {
            public bool success { get; set; }
            public string roleID { get; set; }
            public string userID { get; set; }
            public string deptID { get; set; }
            public Exception error { get; set; }
        }
    }
}
